package com.blank.gpstest;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: blank
 * @date : 2022/7/22
 * @desc : 测试滚动View
 * @url : https://gitee.com/hyx_blank
 */
public class ScrollviewActivity extends AppCompatActivity {
    /**
     * 循环找到ListView最大宽度
     */
    private int getMaxWidth(ListView listView) {
        int maxWidth = 550;

        if (listView.getAdapter() == null) {
            return maxWidth;
        }

        int count = listView.getAdapter().getCount();

        View view = null;
        for (int i = 0; i < count; i++) {
            view = listView.getAdapter().getView(i, null, listView);
            view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            if (view.getMeasuredWidth() > maxWidth) {
                maxWidth = view.getMeasuredWidth();
            }
        }

        return maxWidth;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrollview);
        RecyclerView lv = findViewById(R.id.lv);
        MyListView lv2 = findViewById(R.id.lv2);

        List<Node> list = new ArrayList();
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            Node node = new Node();

            node.setId(String.valueOf(i));
            if (i < 8) {
                node.setName("**" + i + "**");
                strings.add("**" + i + "**");
                node.setpId("");
            } else if (i < 9) {
                strings.add("--" + i + "--" + "**" + i + "**" + "**" + i + "**");
                node.setName("--" + i + "--" + "**" + i + "**" + "**" + i + "**");
                node.setpId("");
            } else if (i < 20) {
                node.setName("--" + i + "--" + "**" + i + "**" + "**" + i + "**");
                node.setpId("1");
            } else if (i < 30) {
                node.setName("//" + i + "//");
                node.setpId("2");
            } else if (i < 40) {
                node.setName("**" + i + "//");
                node.setpId("3");
            } else if (i < 50) {
                node.setName("&&" + i + "//");
                node.setpId("4");
            }
            list.add(node);

        }
//        MyTreeListViewAdapter myAdapter = null;
//        try {
//            myAdapter = new MyTreeListViewAdapter(lv, this, list, 2, true);
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
        MyAdapter myAdapter = new MyAdapter(this);
        lv2.setAdapter(myAdapter);
        myAdapter.setData(strings);

        lv.setLayoutManager(new LinearLayoutManager(this));
        MyReAdapter reAdapter = new MyReAdapter(this);
        lv.setAdapter(reAdapter);
        reAdapter.setData(strings);

//        //创建一个垂直方向的网格布局管理器
//        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(1, LinearLayout.VERTICAL);
//        //设置循环视图的布局管理器
//        lv2.setLayoutManager(manager);
//        MyReAdapter myAdapter = new MyReAdapter(this);
//        lv2.setAdapter(myAdapter);
//        myAdapter.setData(strings);


//        findViewById(R.id.hsc).setMinimumWidth(getMaxWidth(lv2));
    }
}
